package com.ruoyi.web.controller.api.suggest;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.Test.HttpsService;
import com.ruoyi.common.utils.Test.MD5Util;
import com.ruoyi.common.utils.Test.PrivacyBindBodyAxb;
import com.ruoyi.system.domain.PWaitMatters;
import com.ruoyi.system.domain.bo.PWaitMattersBo;
import com.ruoyi.system.domain.vo.PWaitMattersVo;
import com.ruoyi.system.service.IPWaitMattersService;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.apache.commons.codec.binary.Base64;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.Arrays;

/**
 * @Description:
 * @Author: wk
 * @Date: 2022/7/29 15:20
 * @Version: 1.0
 */
@Validated
@Api(value = "待办事项控制器", tags = {"前端-待办事项管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/matter")
public class WaitMattersApiController {
    private final IPWaitMattersService iPWaitMattersService;
    private final ISysUserService iSysUserService;

    @ApiOperation("查询待办事项列表")
    @GetMapping("/list")
    public TableDataInfo<PWaitMattersVo> list(PWaitMattersBo bo, PageQuery pageQuery) {
        return iPWaitMattersService.queryPageList(bo, pageQuery);
    }

    @ApiOperation("新增待办事项")
    @RepeatSubmit()
    @PostMapping("/add")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PWaitMatters bo) {
        iPWaitMattersService.insertByFont(bo);
        return R.ok("新增成功！");
    }

    @ApiOperation("删除待办事项")
    @PostMapping("/del/{ids}")
    public R<Void> remove(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        iPWaitMattersService.deleteWithValidByIds(Arrays.asList(ids), false);
        return R.ok("操作成功！");
    }

    @ApiOperation("公益救助拨号")
    @GetMapping("/publicAssistanceCall")
    public R<String>  publicAssistanceCall(@RequestParam("phone") String phone1) throws Exception {
        Integer miao = null;
        PrivacyBindBodyAxb bindBodyAxb = new PrivacyBindBodyAxb();
        String str = "13041430152";
        /** AXB模式小号绑定接口地址*/
        String url = "https://101.37.133.245:11008/voice/1.0.0/middleNumberAXB";
        /** 设定绑定的隐私小号*/
        String trumpet = str;
        bindBodyAxb.setMiddleNumber(trumpet);
        /** 设定与该隐私小号绑定的号码A*/
        bindBodyAxb.setBindNumberA(phone1);//专家
        /** 设定与该隐私小号绑定的号码B*/
        SysUser user = this.iSysUserService.selectUserById(LoginHelper.getUserId());
        bindBodyAxb.setBindNumberB(user.getPhonenumber());//用户
        /** 设置是否开启通话录音  1:开启，0:关闭*/
        bindBodyAxb.setCallRec(1);
        /** 设置绑定关系有效时长 ,为空表示绑定关系永久，单位:秒*/
        //计算剩余时间秒 按照当前时间
        bindBodyAxb.setMaxBindingTime(30*60);
        //Date date = new Date();
        //SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        //String format = s.format(new Date());
        ////根据订单号查询预约时间
        //Date gettime = date;
        //if(null==gettime){
        //
        //}else {
        //    String format1 = s.format(gettime);
        //    long b = s.parse(format).getTime();
        //    long a = s.parse(format1).getTime();
        //    int c = (int) ((a - b) / 1000);
        //    bindBodyAxb.setMaxBindingTime(c);
        //}
        /** 设置是否透传主叫的号码到A  0:不透传; 1: 透传，不填默认不透传*/
        bindBodyAxb.setPassthroughCallerToA(1);
        /** 设置是否透传主叫的号码到B  0:不透传; 1: 透传，不填默认不透传*/
        bindBodyAxb.setPassthroughCallerToB(1);
        //提示结束时间
        bindBodyAxb.setMaxCallDuration(1800);
//        /** 设置用于接收呼叫结果的服务地址*/
        bindBodyAxb.setCallbackUrl("https://zhjt.cnshangji.cn/api/front/user/expert/resultsOf");
        /** 获取当前系统时间戳*/
        long timestamp = System.currentTimeMillis();
        /** 生成Base64转码后的参数authorization*/
        String name = 567668+":"+timestamp;
        byte[] bytes = name.getBytes();
        String authorization = Base64.encodeBase64String(bytes);
        /** 生成加密参数sig*/
        String sig = MD5Util.getMD5(567668 + "7d22b0d568ba4fe1ac4e842ec98de9cd" + timestamp);
        /** 生成最终接口访问地址*/
        url = url + "/" + 567668 + "/" + sig;
        String body = JSON.toJSONString(bindBodyAxb);
        /** 调用接口*/
        HttpsService httpsService = new HttpsService();
        String result = httpsService.doPost(url, authorization, body, "UTF-8");
        System.out.println("------------------"+result);
        return R.ok(str);
    }
}
